// pages/product/productDetail.js
import { find, del } from "../../api/product";
import { datetimeFormat } from "../../utils/util";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsId: null,
        goodsDetail: {},
        date: '',
        remainingTime: '',
        more: false

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.data.goodsId = options.id;
        console.log(options.id);
    },

    /**
        * 生命周期函数--监听页面显示
        */
    onShow() {
        this.getGoodsDetail(this.data.goodsId)
    },

    getGoodsDetail(id) {
        find(id).then(res => {
            console.log(res.data);
            let date = datetimeFormat(res.data.product.createDate)

            let nowDate = new Date();
            let remainingTime = nowDate.getTime() - res.data.product.createDate;
            remainingTime = res.data.product.limitDay - Math.floor(remainingTime / (24 * 3600 * 1000));

            this.setData({
                goodsDetail: res.data,
                date: date,
                remainingTime: remainingTime
            })
        })
    },

    clickMore() {
        let more = !this.data.more
        this.setData({
            more: more
        })
    },

    editChange() {
        wx.navigateTo({
            url: '/pages/product/editProduct?id=' + this.data.goodsId,
        })
    },

    delChange() {
        let id = this.data.goodsId;
        wx.showModal({
            title: '提示',
            content: '确认删除?',
            success(res) {
                if (res.confirm) {
                    del(id).then(() => {
                        wx.navigateBack();
                    })
                } else if (res.cancel) {
                    return
                }
            }
        });

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
})